﻿namespace Softserve.Pottery.ServiceLocator
{
    using System;
    using System.Collections.Generic;
    using Softserve.Pottery.Infrastructure;
    using Softserve.Pottery.Manufacturing;
    using Softserve.Pottery.Ordering;
    using Softserve.Pottery.Store;
    using Softserve.Pottery.Warehouse;

    public class ServiceLocator : IServiceProvider
    {
        public static ServiceLocator CreateInstance
        {
            get
            {
                if (locator == null)
                {
                    locator = new ServiceLocator();
                    locator.FillServiceMap();
                }
                return locator;
            }
        }

        #region IServiceProvider Members

        public object GetService(Type serviceType)
        {
            return service[serviceType];
        }

        #endregion

        #region Private Methods

        private ServiceLocator()
        {
            service = new Dictionary<Type, object>();
        }

        /// <summary>
        /// HARD CODE, Adding instance of controller. It will load
        /// </summary>
        private void FillServiceMap()
        {
            this.service.Add(typeof(IManufacturingScopeContract), new ManufacturingController(new ManufacturingRepository(), new MessengerControllerBase(ScopeTypes.Manufacturing), this));
            this.service.Add(typeof(IOrderingScopeContract), new OrderingController(new OrderingRepository(), new MessengerControllerBase(ScopeTypes.Ordering), this));
            this.service.Add(typeof(IStoreScopeContract), new StoreController(new StoreRepository(), new MessengerControllerBase(ScopeTypes.Store), this));
            this.service.Add(typeof(IWarehouseScopeContract), new WarehouseController(new WarehouseRepository(), new MessengerControllerBase(ScopeTypes.Warehouse), this));
        }

        #endregion

        #region Private Filds

        private IDictionary<Type, object> service;

        private static ServiceLocator locator;

        #endregion
    }
}
